package com.libgdxbar.pvzlikegame;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.libgdxbar.pvzlikegame.entities.BaseEntity;
import com.libgdxbar.pvzlikegame.objects.BasePlant;
import com.libgdxbar.pvzlikegame.objects.BaseZombie;

import org.libgdxbar.projects.interfaces.renderer.ShapeRenderable;

// 主游戏类
public class MainGame extends Game {
    ShapeRenderer shapeRenderer;
    Viewport viewport;
    Array<BaseEntity> entities;

    @Override
    public void create() {
        shapeRenderer = new ShapeRenderer();
        viewport = new FitViewport(640, 480);
        entities = new Array<>();
        // 这样创建实体 示例植物和示例僵尸都是实体
        entities.add(new BasePlant(100, 100));
        entities.add(new BasePlant(100, 200));
        entities.add(new BasePlant(200, 100));
        entities.add(new BasePlant(200, 200));
        entities.add(new BaseZombie(300, 300));
        entities.add(new BaseZombie(300, 400));
        entities.add(new BaseZombie(400, 300));
        entities.add(new BaseZombie(400, 400));
    }

    @Override
    public void render() {
        ScreenUtils.clear(0f, 0f, 0f, 1f);
        shapeRenderer.setProjectionMatrix(viewport.getCamera().combined);
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(0.15f, 0.15f, 0.2f, 1f);
        shapeRenderer.rect(0, 0, 640, 480);
        for (BaseEntity entity : entities) {
            if (entity instanceof ShapeRenderable) {
                ((ShapeRenderable) entity).render(shapeRenderer);
            }
            entity.run(Gdx.graphics.getDeltaTime());
        }
        shapeRenderer.end();
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
        viewport.update(width, height, true);
    }

    @Override
    public void dispose() {
        shapeRenderer.dispose();
    }
}
